package cn.ytr.project.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import cn.ytr.project.domain.Department;
import cn.ytr.project.mapper.DepartmentMapper;
import cn.ytr.project.service.IDepartmentService;
import cn.ytr.project.util.MybatisUtil;

public class DepartmentServiceImpl implements IDepartmentService{

	@Override
	public List<Department> selectAllForList() {
		SqlSession session = MybatisUtil.getSession();
		DepartmentMapper departmentMapper = session.getMapper(DepartmentMapper.class);
		List<Department> list = departmentMapper.selectAllForList();
		session.close();
		return list;
	}
	
	@Test
	public void test() throws Exception {
		//System.out.println(selectOneAndEmployeesById(2L));
		System.out.println(selectOneAndEmployeesById(2L).getEmployees());
	}

	@Override
	public Department selectOneAndEmployeesById(Long id) {
		SqlSession session = MybatisUtil.getSession();
		DepartmentMapper departmentMapper = session.getMapper(DepartmentMapper.class);
		Department department = departmentMapper.selectOneAndEmployeesById(id);
		session.close();
		return department;
	}

	@Override
	public void update(Department department) {
		SqlSession session = MybatisUtil.getSession();
		DepartmentMapper departmentMapper = session.getMapper(DepartmentMapper.class);
		departmentMapper.update(department);
		session.commit();
		session.close();
	}

	@Override
	public void insert(Department department) {
		SqlSession session = MybatisUtil.getSession();
		DepartmentMapper departmentMapper = session.getMapper(DepartmentMapper.class);
		departmentMapper.insert(department);
		session.commit();
		session.close();
	}

	@Override
	public void delete(Long id) {
		SqlSession session = MybatisUtil.getSession();
		DepartmentMapper departmentMapper = session.getMapper(DepartmentMapper.class);
		departmentMapper.delete(id);
		session.commit();
		session.close();
	}

}
